import 'dart:io';

import 'package:auto_orientation/auto_orientation.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/request/base_request.dart';
import 'package:bxultimate/widgets/video_controls/video_controls.dart';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:video_player/video_player.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

class UtilMk {


  static Future<String?> upload(AssetEntity assets) async {
    Get.back();
    File? file = await assets.file;
    var response = await BaseRequest.uploadFile(file!.path);
    return response.path;
  }

  // 视频播放弹窗
  static showVideoViewer(String url, String title, context) async {
    VideoPlayerController? _controller = VideoPlayerController.network(url,
        videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true));
    await _controller.initialize();
    ChewieController _chewieController = ChewieController(
        videoPlayerController: _controller,
        autoPlay: true,
        allowedScreenSleep: false,
        playbackSpeeds: [0.5, 1, 1.25, 1.5, 2],
        customControls: VideoControls(
          title: title,
          showBackBtn: false,
          // actions: _buildDownload(url, name),
        ),
        routePageBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secondAnimation, provider) {
          // 全屏时，横向视频自动旋转
          final videoWidth =
              provider.controller.videoPlayerController.value.size.width;
          final videoHeight =
              provider.controller.videoPlayerController.value.size.height;
          if (videoWidth > videoHeight) {
            AutoOrientation.landscapeAutoMode(forceSensor: true);
          }
          // 全屏时，横向视频自动旋转
          // chewie _defaultRoutePageBuilder
          return AnimatedBuilder(
            animation: animation,
            builder: (BuildContext context, Widget? child) {
              return Scaffold(
                resizeToAvoidBottomInset: false,
                body: Container(
                  alignment: Alignment.center,
                  color: Colors.black,
                  child: provider,
                ),
              );
            },
          );
        });
    Navigator.of(context).push(TDSlidePopupRoute(
        modalBarrierColor: TDTheme.of(context).fontGyColor2,
        isDismissible: false,
        slideTransitionFrom: SlideTransitionFrom.center,
        builder: (context) {
          return TDPopupCenterPanel(
            closeUnderBottom: true,
            closeClick: () {
              _controller.dispose();
              _chewieController.dispose();
              Navigator.maybePop(context);
            },
            child: AspectRatio(
              aspectRatio: 16 / 9,
              child: Container(
                  color: Colors.black,
                  child: _chewieController != null && _controller != null
                      ? Chewie(
                          controller: _chewieController!,
                        )
                      : Stack(
                          children: const <Widget>[
                            Align(
                              alignment: Alignment.topLeft,
                              child: BackButton(
                                color: Colors.white,
                              ),
                            ),
                            Center(
                              child: CircularProgressIndicator(),
                            )
                          ],
                        )),
            ),
          );
        }));
  }

  static KeyboardActionsConfig getKeyboardActionsConfig(
      BuildContext context, List<FocusNode> list) {
    return KeyboardActionsConfig(
      // keyboardBarColor: MkThemeUtils.getKeyboardActionsColor(context),
      actions: List.generate(
          list.length,
          (i) => KeyboardActionsItem(
                focusNode: list[i],
                toolbarButtons: [
                  (node) {
                    return GestureDetector(
                      onTap: () => node.unfocus(),
                      child: Padding(
                        padding: const EdgeInsets.only(right: 16.0),
                        child: Text('关闭'),
                      ),
                    );
                  },
                ],
              )),
    );
  }

  static void openBottomSheet(double height, String title, Widget child) {
    Get.bottomSheet(
      Container(
        width: Get.width,
        height: height,
        margin: EdgeInsets.symmetric(horizontal: 12),
        child: Column(
          children: [
            Semantics(
              button: true,
              label: '返回',
              child: Container(
                key: const Key('widget_move_modal_bottom_sheet'),
                margin: EdgeInsets.all(12),
                height: 4,
                width: 30,
                decoration: BoxDecoration(
                  color: const Color(0xFF2B3034),
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
            ),
            title != ''
                ? Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(
                          child: Text(title,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                  fontSize: 16, fontWeight: FontWeight.bold))),
                      // Container(
                      //   width: 26,
                      //   height: 26,
                      //   decoration: BoxDecoration(
                      //       color: Color(0xFFEEEEEE),
                      //       borderRadius: BorderRadius.all(Radius.circular(26))),
                      //   child: GestureDetector(
                      //     onTap: () {
                      //       Get.back();
                      //     },
                      //     child: Icon(
                      //       Icons.close,
                      //       color: Colors.black,
                      //       size: 14,
                      //     ),
                      //   ),
                      // )
                    ],
                  )
                : SizedBox(),
            Gaps.vGap8,
            child
          ],
        ),
      ),
      isScrollControlled: true,
      barrierColor: Colors.black26,
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(32),
          topRight: Radius.circular(32),
        ),
      ),
    );
  }

  // 打电话
  static Future<bool> call(String phone) async {
    Uri uri = Uri.parse('tel:$phone');
    return await launchUrl(uri);
  }
}
